package emotion_codesoe_com

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/emotion_codesoe_com"
	emotion_codesoe_comReq "github.com/flipped-aurora/gin-vue-admin/server/model/emotion_codesoe_com/request"
)

type OsBannerService struct {
}

// CreateOsBanner 创建osBanner表记录
// Author [piexlmax](https://github.com/piexlmax)
func (osBannerService *OsBannerService) CreateOsBanner(osBanner *emotion_codesoe_com.OsBanner) (err error) {
	err = global.GVA_DB.Create(osBanner).Error
	return err
}

// DeleteOsBanner 删除osBanner表记录
// Author [piexlmax](https://github.com/piexlmax)
func (osBannerService *OsBannerService) DeleteOsBanner(id string) (err error) {
	err = global.GVA_DB.Delete(&emotion_codesoe_com.OsBanner{}, "id = ?", id).Error
	return err
}

// DeleteOsBannerByIds 批量删除osBanner表记录
// Author [piexlmax](https://github.com/piexlmax)
func (osBannerService *OsBannerService) DeleteOsBannerByIds(ids []string) (err error) {
	err = global.GVA_DB.Delete(&[]emotion_codesoe_com.OsBanner{}, "id in ?", ids).Error
	return err
}

// UpdateOsBanner 更新osBanner表记录
// Author [piexlmax](https://github.com/piexlmax)
func (osBannerService *OsBannerService) UpdateOsBanner(osBanner emotion_codesoe_com.OsBanner) (err error) {
	err = global.GVA_DB.Save(&osBanner).Error
	return err
}

// GetOsBanner 根据id获取osBanner表记录
// Author [piexlmax](https://github.com/piexlmax)
func (osBannerService *OsBannerService) GetOsBanner(id string) (osBanner emotion_codesoe_com.OsBanner, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&osBanner).Error
	return
}

// GetOsBannerInfoListPage 分页获取osBanner表记录
// Author [piexlmax](https://github.com/piexlmax)
func (osBannerService *OsBannerService) GetOsBannerInfoListPage(info emotion_codesoe_comReq.OsBannerSearch) (list []emotion_codesoe_com.OsBanner, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&emotion_codesoe_com.OsBanner{})
	var osBanners []emotion_codesoe_com.OsBanner
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Title != "" {
		db = db.Where("title LIKE ?", "%"+info.Title+"%")
	}
	if info.Module != nil {
		db = db.Where("module = ?", info.Module)
	}
	if info.Block != nil {
		db = db.Where("block = ?", info.Block)
	}
	if info.SkipMode != nil {
		db = db.Where("skip_mode = ?", info.SkipMode)
	}
	if info.Status != nil {
		db = db.Where("status = ?", info.Status)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&osBanners).Error
	return osBanners, total, err
}

// GetOsBannerInfoList 分页获取osBanner表记录
// Author [piexlmax](https://github.com/piexlmax)
func (osBannerService *OsBannerService) GetOsBannerInfoList(info emotion_codesoe_comReq.OsBannerSearch) (list []emotion_codesoe_com.OsBanner, err error) {

	// 创建db
	db := global.GVA_DB.Model(&emotion_codesoe_com.OsBanner{})
	var osBanners []emotion_codesoe_com.OsBanner
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Title != "" {
		db = db.Where("title LIKE ?", "%"+info.Title+"%")
	}
	if info.Module != nil {
		db = db.Where("module = ?", info.Module)
	}
	if info.Block != nil {
		db = db.Where("block = ?", info.Block)
	}
	if info.SkipMode != nil {
		db = db.Where("skip_mode = ?", info.SkipMode)
	}
	if info.Status != nil {
		db = db.Where("status = ?", info.Status)
	}
	if err != nil {
		return
	}

	err = db.Order("sort DESC").Find(&osBanners).Error
	return osBanners, err
}
